/*
 * MainFrame.java
 *
 * Created on September 18, 2007, 4:15 PM
 */

package jia.editor;

import java.awt.Dimension;
import java.net.MalformedURLException;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import jia.core.GameStateManager;
import jia.core.InputProcessor;
import jia.core.state.BasicEditorStateManager;
import jia.editor.dialogues.AboutJia;
import jia.editor.dialogues.HelpViewer;
import jia.editor.processors.EditorInputProcessor;
import jia.game.FirstPersonViewport;
import jia.game.GenericFlags;
import jia.game.TestGame;
import jia.game.Viewport;
import org.apache.log4j.Logger;

/**
 *
 * @author  estell
 */
public class EditorMainFrame extends javax.swing.JFrame implements EditorUi{
    private static final Logger log = Logger.getLogger(EditorMainFrame.class);
    public static ModuleEditorPanel editPanel;
    public static Viewport fpView;
    public static InputProcessor inputProc;
    public static GameStateManager gameState;
    
    public void dispose() {
        try {
            
            DriverManager.getConnection("jdbc:derby:;shutdown=true;");
        } catch (SQLException ex) {
            if (ex.getSQLState().equals("XJ015")){
                if (GenericFlags.warningMessages) {
                    log.warn("Database shut down normally");
                }
            }else{
                if (GenericFlags.errorMessages){
                    log.error("Database was not shut down normally, using this error code: " + ex.getSQLState());
                }
            }
        }
        super.dispose();
        System.exit(0);
        
    }
    
    /** Creates new form MainFrame */
    public EditorMainFrame() {
        initComponents();
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        inputProc = new EditorInputProcessor();
        
        editPanel = new ModuleEditorPanel();
        editPanel.setSize(new Dimension(600, 600));
        fpView = new FirstPersonViewport();
        TestGame game = null;
        try {
            game = new TestGame();
        } catch (MalformedURLException ex) {
            if (GenericFlags.errorMessages){
                log.error(ex.getMessage());
            }
        }
        gameState = new BasicEditorStateManager();
        gameState.getPlayerState().setCurrentModule(game.getTestModule());
        inputProc.registerGameState(gameState);
        editPanel.updateGameState(gameState);
        fpView.updateGameState(gameState);
        
        addKeyListener(new EditorKeyListener(inputProc, this));
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JIA - Java Infinite Adventures");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setLocationByPlatform(true);
        setResizable(false);
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 810, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 610, Short.MAX_VALUE)
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                EditorMainFrame mainframe = new EditorMainFrame();
                mainframe.setVisible(true);
                
                
            }
        });
    }
    
    public void updateState() {
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
    
    
}
